import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:qaun_ni_dev/utils/request.dart';
import 'package:qaun_ni_dev/model/attributeModel.dart';
import 'package:qaun_ni_dev/utils/toast.dart';

class AttributeProvide with ChangeNotifier {

  List<AttributeData> attributeListData = [];
  int page = 1;
  var attributeCode = '';
  var attributeType = '';

  getAttributeList (context) async{
    page = 1;
    attributeCode = '';
    attributeType = '';
    attributeListData = [];
    var formData = {
      'currentPage':page,
      'code': attributeCode,
      'type': attributeType
    };
    await requestPost(context,'/appProperty/list',formData:formData).then((value) {
      var res = json.decode(value.toString());
      var tempData = AttributeModel.fromJson(res);
      attributeListData = tempData.data;
    });
    notifyListeners();
  }

  getAttributeMoreList (context) async{
    page++;
    var formData = {
      'currentPage':page,
      'code': attributeCode,
      'type': attributeType
    };
    await requestPost(context,'/appProperty/list',formData:formData).then((value) {
      var res = json.decode(value.toString());
      if(res['data'].length > 0) {
        var tempData = AttributeModel.fromJson(res);
        var tempDatas = tempData.data;
        attributeListData.addAll(tempDatas);
      } else {
        Toast.toast(context,msg: '已经没有数据了哦');
      }
    });
    notifyListeners();
  }

  // 删除
  deleteAttribute(context,val) async{
    var formData = {
      'propertyId':val
    };

    await requestPost(context,'/appProperty/del',formData:formData).then((value) {
      var res = json.decode(value.toString());
      if(res['code'] == 200) {
        Toast.toast(context,msg: '数据删除成功');
        Navigator.pushNamed(context, '/attributePage');
      }
    });
    notifyListeners();
  }

  // 更新
  updateAttribute(context,formData) async {
    await requestPost(context,'/appProperty/update',formData:formData).then((value){
      var res = json.decode(value.toString());
      if(res['code'] == 200) {
        Toast.toast(context,msg: '数据更新成功');
        Navigator.pushNamed(context, '/attributePage');
      } else {
        Toast.toast(context,msg: '数据更新失败');
      }
    });
    notifyListeners();
  }

  // 添加
  addAttribute(context,formData) async {

    await requestPost(context,'/appProperty/add',formData:formData).then((value){
      var res = json.decode(value.toString());
      if(res['code'] == 200) {
        Toast.toast(context,msg: '新增数据成功');
        Navigator.pushNamed(context, '/attributePage');
      } else {
        Toast.toast(context,msg: '新增数据失败');
      }
    });

  }


  // 获取code type值
  changeGetSearch(valueCode,valueType) {
    attributeCode = valueCode;
    attributeType = valueType;
    notifyListeners();
  }



}



